package com.lm.pf4j.controller;

import com.lm.pf4j.service.Greeting;
import org.pf4j.PluginManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class GreetingController {

    private final PluginManager pluginManager;

    public GreetingController(PluginManager pluginManager) {
        this.pluginManager = pluginManager;
    }

    @GetMapping("/greet")
    public String greet(String name) {
        List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
        if (greetings.isEmpty()) {
            return "No Greeting plugin found!";
        }
        return greetings.get(0).sayHello(name);
    }
}